﻿// JSON C# Class Generator
// http://at-my-window.blogspot.com/?page=json-class-generator

using System;
using Newtonsoft.Json.Linq;
using JsonCSharpClassGenerator;
using TomatoSynchronizer.Store;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace TomatoSynchronizer.Entities
{

	public class WeightClass
	{
		public override bool Equals(object obj)
		{
			if(obj is WeightClass)
			{
				WeightClass wclass = (WeightClass)obj;
				if(wclass._is_default == _is_default && wclass._weight_class_key == _weight_class_key && wclass._weight_class_title == _weight_class_title)
				{
					foreach(WeightClassRule ruleSrc in wclass.rules)
					{
						foreach(WeightClassRule ruleDest in _rules)
						{
							if(ruleSrc.weight_class_title == ruleDest.weight_class_title)
							{
								if(!ruleSrc.Equals(ruleDest))
								{
									return false;
								}
							}
						}
					}
					
					return true;
				}
			}
			
			return false;
		}

		public WeightClass()
		{
		}

		public WeightClass(string json)
			: this(JObject.Parse(json))
		{
		}

		private JObject __jobject;
		public WeightClass(JObject obj)
		{
			this.__jobject = obj;
		}

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		private WeightClassStore store;

		public WeightClassStore Store
		{
			get{
				return store;}
			set{
				store = value;
				var cles = keys;
				var noms = names;
			}
		}

		[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		private string _weight_class_id;
		public string weight_class_id
		{
			get
			{
				if(_weight_class_id == null)
					_weight_class_id = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "weight_class_id"));
				return _weight_class_id;
			}
			set
			{
				_weight_class_id = value;
			}
		}

		[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		private string _weight_class_key;
		public string weight_class_key
		{
			get
			{
				if(_weight_class_key == null)
					_weight_class_key = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "weight_class_key"));
				return _weight_class_key;
			}
			set
			{
				_weight_class_key = value;
			}
		}

		[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		private string _language_id;
		public string language_id
		{
			get
			{
				if(_language_id == null)
					_language_id = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "language_id"));
				return _language_id;
			}
			set
			{
				_language_id = value;
			}
		}

		[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		private string _weight_class_title;
		public string weight_class_title
		{
			get
			{
				if(_weight_class_title == null)
					_weight_class_title = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "weight_class_title"));
				return _weight_class_title;
			}
			set
			{
				_weight_class_title = value;
			}
		}

		[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		private Dictionary<string, string> _names;
		public Dictionary<string, string> names
		{
			get
			{
				if(_names == null)
				{
					_names = new Dictionary<string, string>();
					
					if(__jobject != null)
					{
						if(store != null)
						{
							var languages = store.site.GetLanguageStore().LoadItems();
							
							foreach(Language language in languages)
							{
								_names["name[" + language.languages_id + "]"] = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject,"name[" + language.languages_id + "]"));
							}
						}
					}
				}
				
				return _names;
			}
			set
			{
				_names = value;
			}
		}

		[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		private Dictionary<string, string> _keys;
		public Dictionary<string, string> keys
		{
			get
			{
				if(_keys == null)
				{
					_keys = new Dictionary<string, string>();
					
					if(__jobject != null)
					{
						if(store != null)
						{
							var languages = store.site.GetLanguageStore().LoadItems();
							
							foreach(Language language in languages)
							{
								_keys["key[" + language.languages_id + "]"] = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject,"key[" + language.languages_id + "]"));
							}
						}
					}
				}
				
				return _keys;
			}
			set
			{
				_keys = value;
			}
		}

		[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		private WeightClassRule[] _rules;
		public WeightClassRule[] rules
		{
			get
			{
				if(_rules == null && __jobject != null)
					_rules = (WeightClassRule[])JsonClassHelper.ReadArray<WeightClassRule>(JsonClassHelper.GetJToken<JArray>(__jobject, "rules"), JsonClassHelper.ReadStronglyTypedObject<WeightClassRule>, typeof(WeightClassRule[]));
				return _rules;
			}
			set
			{
				_rules = value;
			}
		}
		
		private bool _is_default;
		public bool is_default
		{
			get
			{
				if(!_is_default && __jobject != null)
				{
					//this field does not exists in the json data when the current WeightClass is not the default one
					try
					{
						_is_default = JsonClassHelper.ReadBoolean(JsonClassHelper.GetJToken<JValue>(__jobject, "is_default"));
					}
					catch(Newtonsoft.Json.JsonSerializationException)
					{
						_is_default = false;
					}
					catch(Exception ex)
					{
						throw ex;
					}
				}
				
				return _is_default;
			}
			set
			{
				_is_default = value;
			}
		}

	}
}
